package com.guli.gulimall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.guli.gulimall.product.entity.AttrAttrgroupRelationEntity;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;



/**
 * 商品属性
 *
 * @author wangguohai
 * @email 270901876@qq.com
 * @date 2022-02-21 16:38:49
 */
@RestController
@RequestMapping("/product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
    public R list(@PathVariable("type")String type,
                  @RequestParam Map<String, Object> params,
                  @PathVariable("catelogId")Long catelogId){
        PageUtils page = attrService.queryPage(type,params,catelogId);

        return R.ok().put("page", page);
    }

    @RequestMapping("/noattr/relation")
    public R noattrRelation(
                  @RequestParam Map<String, Object> params
                 ){
        PageUtils page = attrService.noattrRelation(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/{groupId}/attr/relation")
    public R list(@PathVariable("groupId")String groupId,
                  @RequestParam Map<String, Object> params
                 ){
        PageUtils page = attrService.attrRelation(groupId,params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return R.ok();
    }
    @RequestMapping("/attr/relation")
    public R update(@RequestBody List<AttrEntity> attrs){
		attrService.updateBatchById(attrs);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody List<AttrAttrgroupRelationEntity> attrs){

        ArrayList<AttrEntity> list = new ArrayList<>();
        for (AttrAttrgroupRelationEntity attr : attrs) {
            AttrEntity byId = attrService.getById(attr.getAttrId());
            byId.setAttrGroupId(0L);
            byId.setAttrGroupName("");
            list.add(byId);

        }
        attrService.attrRelationDelete(list);

        return R.ok();
    }

}
